<?php

include_once dirname(dirname(__FILE__)) . '/include/common.php';
$core = new core();
$zen_customer_order_detail_id = isset($_POST["id"]) ? intval($_POST["id"]) : 0;
$status = isset($_POST["status"]) ? intval($_POST["status"]) : 0;
$zen_customer_order_detail = $core->getZenCustomerOrderDetailById($zen_customer_order_detail_id);
if ($zen_customer_order_detail_id > 0 && $zen_customer_order_detail) {
    $exchange = $core->db->get_row("SELECT * FROM zen_exchange ORDER BY id DESC LIMIT 1");
    $exchange_value = $exchange->exchange;

    $old_order_status = $zen_customer_order_detail->order_status;
    $price =$zen_customer_order_detail->product_price;
    $store_price = $zen_customer_order_detail->store_price;
    $stamp_price = $zen_customer_order_detail->stamp_price;
    if ($status >= 0 && $status < 9) {
        $reload_data= 0;
        if ($status == 6) {
            $reload_data=1;
            //het hang, product price =0, update total_current_money,total_order_money
            $zen_customer_order = $core->getZenCustomerOrderById($zen_customer_order_detail->customer_order_id);
            if($zen_customer_order) {
//                $percent_service = $zen_customer_order->service_percent;
//                $weight = $zen_customer_order->weight;
//                $fee_weight = $zen_customer_order->fee_per_kg;
//                $product_price = $zen_customer_order_detail->product_price;
//                $product_num = $zen_customer_order_detail->product_num;
//                $product_pt = $zen_customer_order_detail->store_price;
//                $product_at = $zen_customer_order_detail->stamp_price;
//                $total_at = $zen_customer_order->total_stamp_price - $product_at*$product_num;
//                $total_pt = $zen_customer_order->total_store_price - $product_pt*$product_num;
//
//                switch($zen_customer_order->type_calculate_payment){
//                    case 'normal':
//                        $tmp_money = $product_price * $product_num;
//                        //tong tien order giam
//                        $total_order_money = $zen_customer_order->total_order_money - $tmp_money;
//                        break;
//                    case 'at':
//                        //tong tien order giam
//                        $total_order_money = $total_at*$exchange_value;
//                        break;
//                    case 'pt':
//                        //tong tien order giam
//                        //tong PT + 5%PT + 12EU*weight
//                        if($total_pt > 0){
//                            $total_order_money = $exchange_value*($total_pt + $total_pt*$percent_service/100 + $fee_weight*$weight);
//                        }else{
//                            $total_order_money = 0;
//                        }
//
//                        break;
//                }
//
//                //tinh lai so tien trong tai khoan khach hang
//                $total_current_money = $zen_customer_order->total_payment - $total_order_money;
//
//                $core->db->query("UPDATE zen_customer_order SET total_order_money=$total_order_money,
//                                                                total_current_money= $total_current_money,
//                                                                 total_store_price = $total_pt,
//                                                                total_stamp_price = $total_at
//                                  WHERE customer_order_id=" . $zen_customer_order->customer_order_id);


                $core->db->query("UPDATE zen_customer_order_detail SET order_status=$status,empty_date=now()
                                  WHERE id=$zen_customer_order_detail_id");

                //đồng bộ lại tiền hàng của khách sau khi sản phẩm hết hàng

                $sync = $core->syncCustomerOrderMoney($zen_customer_order_detail->customer_order_id);

                //gia tri tra ve client
                $price = 0;
                $stamp_price = 0;
                $store_price = 0;
            }else{
                echo json_encode(array('type' => 1, 'msg' => "update trạng thái thất bại!"));
                die;
            }
        } else {
            if ($old_order_status == 6) {
                $reload_data= 1;
                //chuyen trang thai ra khoi trang thai het han, update lai product price, total..
                $zen_customer_order = $core->getZenCustomerOrderById($zen_customer_order_detail->customer_order_id);
                if($zen_customer_order) {
//                    $store_price = $zen_customer_order_detail->store_price;
//                    $stamp_price = $zen_customer_order_detail->stamp_price;
//                    $price = $zen_customer_order_detail->product_price;
//
//                    $percent_service = $zen_customer_order->service_percent;
//                    $weight = $zen_customer_order->weight;
//                    $fee_weight = $zen_customer_order->fee_per_kg;
//                    $product_price = $zen_customer_order_detail->product_price;
//                    $product_num = $zen_customer_order_detail->product_num;
//                    $product_pt = $zen_customer_order_detail->store_price;
//                    $product_at = $zen_customer_order_detail->stamp_price;
//                    $total_at = $zen_customer_order->total_stamp_price + $product_at*$product_num;
//                    $total_pt = $zen_customer_order->total_store_price + $product_pt*$product_num;
//
//                    switch($zen_customer_order->type_calculate_payment){
//                        case 'normal':
//                            $tmp_money = $product_price * $product_num;
//                            //tong tien order giam
//                            $total_order_money = $zen_customer_order->total_order_money + $tmp_money;
//                            break;
//                        case 'at':
//                            //tong tien order giam
//                            $total_order_money = $total_at*$exchange_value;
//                            break;
//                        case 'pt':
//                            //tong tien order giam
//                            //tong PT + 5%PT + 12EU*weight
//                            if($total_pt > 0){
//                                $total_order_money = $exchange_value*($total_pt + $total_pt*$percent_service/100 + $fee_weight*$weight);
//                            }else{
//                                $total_order_money = 0;
//                            }
//
//                            break;
//                    }
//
//                    //tinh lai so tien trong tai khoan khach hang
//                    $total_current_money = $zen_customer_order->total_payment - $total_order_money;
//
//                    $core->db->query("UPDATE zen_customer_order SET total_order_money=$total_order_money,
//                                                                    total_current_money= $total_current_money,
//                                                                    total_store_price = $total_pt,
//                                                                    total_stamp_price = $total_at
//                                      WHERE customer_order_id=" . $zen_customer_order->customer_order_id);

                    switch ($status) {
                        case 0:
                            $core->db->query("UPDATE zen_customer_order_detail SET order_status=$status,store_date = now()
                                            WHERE id=$zen_customer_order_detail_id");
                            break;
                        case 1:
                            $core->db->query("UPDATE zen_customer_order_detail SET order_status=$status,online_date = now()
                                            WHERE id=$zen_customer_order_detail_id");
                            break;
                        case 3:
                            $core->db->query("UPDATE zen_customer_order_detail SET order_status=$status,received_date = now()
                                            WHERE id=$zen_customer_order_detail_id");
                            break;
                        case 5:
                            $core->db->query("UPDATE zen_customer_order_detail SET order_status=$status,saled_date = now()
                                            WHERE id=$zen_customer_order_detail_id");
                            break;
                        case 7:
                            $core->db->query("UPDATE zen_customer_order_detail SET order_status=$status,customer_order_date = now()
                                            WHERE id=$zen_customer_order_detail_id");
                            break;
                        default :
                            $core->db->query("UPDATE zen_customer_order_detail SET order_status=$status
                                            WHERE id=$zen_customer_order_detail_id");
                            break;
                    }

                    //đồng bộ lại tiền hàng của khách sau khi sản phẩm hết hàng

                    $sync = $core->syncCustomerOrderMoney($zen_customer_order_detail->customer_order_id);

                }else{
                    echo json_encode(array('type' => 1, 'msg' => "update trạng thái thất bại!"));
                    die;
                }
            } else {
                switch ($status) {
                    case 0:
                        $core->db->query("UPDATE zen_customer_order_detail SET order_status=$status,store_date = now()
                                            WHERE id=$zen_customer_order_detail_id");
                        break;
                    case 1:
                        $core->db->query("UPDATE zen_customer_order_detail SET order_status=$status,online_date = now()
                                            WHERE id=$zen_customer_order_detail_id");
                        break;
                    case 3:
                        $core->db->query("UPDATE zen_customer_order_detail SET order_status=$status,received_date = now()
                                            WHERE id=$zen_customer_order_detail_id");
                        break;
                    case 5:
                        $core->db->query("UPDATE zen_customer_order_detail SET order_status=$status,saled_date = now()
                                            WHERE id=$zen_customer_order_detail_id");
                        break;
                    case 7:
                        $core->db->query("UPDATE zen_customer_order_detail SET order_status=$status,customer_order_date = now()
                                            WHERE id=$zen_customer_order_detail_id");
                        break;
                    default :
                        $core->db->query("UPDATE zen_customer_order_detail SET order_status=$status
                                            WHERE id=$zen_customer_order_detail_id");
                        break;
                }
            }
        }
        //update common order status of customer
        //update trang thai het hang
//         $core->db->query("UPDATE zen_customer_order 
//SET customer_order_status = (CASE (SELECT COUNT(id) FROM zen_customer_order_detail zcod WHERE zcod.customer_order_id=".$zen_customer_order_detail->customer_order_id.")
//       WHEN (SELECT COUNT(id) FROM zen_customer_order_detail zcod WHERE zcod.customer_order_id=".$zen_customer_order_detail->customer_order_id." AND order_status =6 ) THEN 6 END
//) WHERE customer_order_id=".$zen_customer_order_detail->customer_order_id);
//         
//         //check các trang thai khac
//        $core->db->query("UPDATE zen_customer_order 
//SET customer_order_status = (CASE (SELECT COUNT(id) FROM zen_customer_order_detail zcod WHERE zcod.customer_order_id=".$zen_customer_order_detail->customer_order_id." AND order_status != 6)
//     WHEN (SELECT COUNT(id) FROM zen_customer_order_detail zcod WHERE zcod.customer_order_id=".$zen_customer_order_detail->customer_order_id." AND order_status=0 ) THEN 0
//     WHEN (SELECT COUNT(id) FROM zen_customer_order_detail zcod WHERE zcod.customer_order_id=".$zen_customer_order_detail->customer_order_id." AND order_status=1 ) THEN 1
//      WHEN (SELECT COUNT(id) FROM zen_customer_order_detail zcod WHERE zcod.customer_order_id=".$zen_customer_order_detail->customer_order_id." AND order_status=2 ) THEN 2
//       WHEN (SELECT COUNT(id) FROM zen_customer_order_detail zcod WHERE zcod.customer_order_id=".$zen_customer_order_detail->customer_order_id." AND order_status=3 ) THEN 3
//      WHEN (SELECT COUNT(id) FROM zen_customer_order_detail zcod WHERE zcod.customer_order_id=".$zen_customer_order_detail->customer_order_id." AND order_status=4 ) THEN 4
//      WHEN (SELECT COUNT(id) FROM zen_customer_order_detail zcod WHERE zcod.customer_order_id=".$zen_customer_order_detail->customer_order_id." AND order_status=5 ) THEN 5
//
//        WHEN (SELECT COUNT(id) FROM zen_customer_order_detail zcod WHERE zcod.customer_order_id=".$zen_customer_order_detail->customer_order_id." AND order_status=7 ) THEN 7
//      WHEN (SELECT COUNT(id) FROM zen_customer_order_detail zcod WHERE zcod.customer_order_id=".$zen_customer_order_detail->customer_order_id." AND order_status=8 ) THEN 8
//     ELSE 9 END
//) WHERE customer_order_id=".$zen_customer_order_detail->customer_order_id);
       
        $text_order_status = '';
        switch ($zen_customer_order_detail->order_status):
            case 0: $text_order_status = "Store";
                break;
            case 1: $text_order_status = "Online";
                break;
            case 2: $text_order_status = "Hàng có sẵn";
                break;
            case 3: $text_order_status = "Đã nhận hàng";
                break;
            case 4: $text_order_status = "Đang shipping";
                break;
            case 5: $text_order_status = "Khách đã nhận";
                break;
            case 6: $text_order_status = "Hết hàng";
                break;
            case 7: $text_order_status = "ORDER";
                break;
            case 8: $text_order_status = "Chưa chắc chắn";
                break;

        endswitch;
        $zen_customer_order = $core->getZenCustomerOrderById($zen_customer_order_detail->customer_order_id);
        echo json_encode(array('type' => 0, 'msg' => "update trạng thái thành công!", 
            "status" => $text_order_status,'reload_data'=>$reload_data,'customer_order_id'=>$zen_customer_order_detail->customer_order_id,
            'price'=>number_format($price/1000),
            'store_price'=>number_format($store_price),
            'stamp_price'=>number_format($stamp_price),
            'select_customer_order_status'=>$zen_customer_order->customer_order_status));
        die;
    } else {
        echo json_encode(array('type' => 1, 'msg' => "update trạng thái thất bại!"));
        die;
    }
} else {
    echo json_encode(array('type' => 1, 'msg' => "update trạng thái thất bại!"));
    die;
}
?>